package com.hh.demo.mq;

import com.hh.demo.domain.MessageEmail;
import com.hh.demo.service.IMessageEmailService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import static com.hh.demo.constants.MqConstants.*;

/**
 * @author 黄浩
 * @create 2022-07-25 22:33
 */
@Component
@RocketMQMessageListener(consumerGroup = "service-common-emailmessage-consumer",
        topic = COURSE_EMAIL_TOPIC,
        selectorExpression = COURSE_EMAIL_TAG
)
public class EmailMessageMQConsumer implements RocketMQListener<MessageExt> {

    @Resource
    private IMessageEmailService messageEmailService;

    @Override
    public void onMessage(MessageExt messageExt) {
        String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        MessageEmail messageEmail = new MessageEmail();
        messageEmail.setEmail("352782895@qq.com");
        messageEmail.setTitle("课程上线通知");
        messageEmail.setContent(message);
        messageEmail.setSendTime(new Date());
        messageEmailService.insert(messageEmail);
    }
}
